package com.nf.mvc.file;

import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.nf.mvc.util.FileUtils.copy;

public class StandardMultipartFile implements MultipartFile {

    private Part part;

    public StandardMultipartFile(Part part) {
        this.part = part;
    }

    @Override
    public String getName() {
        return part.getName();
    }

    @Override
    public String getOriginalFilename() {
        return part.getSubmittedFileName();
    }

    @Override
    public void transferTo(Path dest) throws IOException {
        copy(part.getInputStream(), Files.newOutputStream(dest));
    }

    @Override
    public void transferTo(String realPath) throws IOException {
        transferTo(Paths.get(realPath));
    }
}
